<?php

class Concierge extends Users
{

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Admin the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return '{{users}}';
    }

    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('email', 'validate_email'),
            array('email', 'email'),
            array('firstname,lastname,email', 'required'),
            array('status,address,address2,business_name,business_slogan,facebook,twitter,gplus,instagram,website,business_logo,business_description,postal_code,birthday,city,country,home_phone,mobile_phone,office_phone,fax,photo,gender,occupation,note, firstname', 'safe'),
            array('repassword, new_pass', 'length', 'min' => 6),
            array('repassword', 'compare', 'compareAttribute' => 'new_pass'),
        );
    }

    public function getUrlEdit()
    {
        return Yii::app()->createUrl('concierge/edit', array('pk' => $this->ukey));
    }

    //validate
    public function validate_email($attribute, $param)
    {

        if ($this->isNewRecord)
        {

            $exits_merchant = Users::check_exist_by_email($this->email, Users::TYPE_CONCIERGE);

            if ($exits_merchant)
            {
                $this->addError($attribute, $this->email . ' has taken');
            }
        }
        else
        {// edit
            $exits_merchant = Users::model()->find('email=:email AND id!=:id AND type=:type', array(
                ':email' => $this->email,
                ':id'    => $this->id,
                ':type'  => Users::TYPE_CONCIERGE,
            ));
            if ($exits_merchant)
            {
                $this->addError($attribute, $this->email . ' has taken');
            }
        }
    }

    public function completeCreateUser()
    {
        $this->updateRole();
        $this->ukey = $this->genUkey();
        $this->update(array('ukey'));
    }

    function updateRole()
    {
        $auth = Yii::app()->authManager;

        // Xóa các quyền đã gán trước đây đi
        if ($auth->getAuthAssignments($this->id))
            foreach ($auth->getAuthAssignments($this->id) as $item)
                $auth->revoke($item->itemName, $this->id);

        // Gán quyền mới vào
        $auth->assign($this->type, $this->id);
    }

    public function afterSave()
    {
        parent::afterSave();
        if ($this->isNewRecord)
        {
            if ($this->new_pass == '')
            {
                $this->salt     = $this->generateSalt();
                $this->new_pass = Utils::genRandomString(6);
                $this->password = $this->hashPassword($this->new_pass, $this->salt);
            }
            else
            {

                $this->salt     = $this->generateSalt();
                $this->password = $this->hashPassword($this->new_pass, $this->salt);
            }
        }
        else
        {
            if ($this->new_pass != '')
            {
                $this->salt     = $this->generateSalt();
                $this->password = $this->hashPassword($this->new_pass, $this->salt);
                if ($this->saveAttributes(array('salt', 'password')))
                {
                    try
                    {
                        $this->updateRole();
                        MyEmail::sendToNewUser($this, $this->new_pass);
                    }
                    catch (Exception $ex)
                    {
                        
                    }
                }
            }
            $this->updateRole();
        }

        return TRUE;
    }

}
